package com.example.qmapp

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.View
import android.widget.TextView


import androidx.appcompat.app.AppCompatActivity
import android.widget.ImageView
import com.google.zxing.BarcodeFormat
import com.journeyapps.barcodescanner.DefaultDecoderFactory
import com.journeyapps.barcodescanner.DecoratedBarcodeView

import android.view.GestureDetector
import android.view.MotionEvent
class DetailActivity() : AppCompatActivity() {
    private var startX: Float = 0f // 声明并初始化 startX 变量
    @Deprecated("Deprecated in Java")
    override fun onBackPressed() {
        // 在此处添加逻辑，比如判断是否在扫描状态，如果是则处理扫描结束逻辑，否则执行返回操作
        if (barcodeScannerView.visibility == View.VISIBLE) {
            barcodeScannerView.visibility = View.INVISIBLE
            // 处理扫描结束逻辑
        }else{
            moveTaskToBack(true)
//            super.onBackPressed()
        }
    }
    private lateinit var barcodeScannerView: DecoratedBarcodeView
    // 在 MainActivity 类中添加一个变量用于存储TextView
    private lateinit var resultTextView: TextView
    @SuppressLint("CutPasteId", "MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.detailpage)
        barcodeScannerView = findViewById(R.id.barcodeScannerView)
        resultTextView = findViewById(R.id.resultTextView)
        setupScanner()

        val scanImageView: ImageView = findViewById(R.id.imageView5)
        scanImageView.setOnClickListener {
            barcodeScannerView.visibility = View.VISIBLE
            startScan()
        }

    }
    private fun setupScanner() {
        val formats = listOf(BarcodeFormat.QR_CODE)

        val decoderFactory = DefaultDecoderFactory(formats)

        barcodeScannerView.barcodeView.decoderFactory = decoderFactory
        barcodeScannerView.setStatusText("Place a QR code inside the viewfinder rectangle to scan.")

        barcodeScannerView.decodeSingle { result ->
            // 处理扫描到的结果
            runOnUiThread {
                resultTextView.text = result.text
                barcodeScannerView.visibility = View.INVISIBLE
                // 更新UI展示扫描结果
            }
        }

        // 不在这里启动扫描
    }

    private fun startScan() {
        barcodeScannerView.resume()
    }

    override fun onResume() {
        super.onResume()
        barcodeScannerView.resume()
    }

    override fun onPause() {
        super.onPause()
        barcodeScannerView.pause()
    }

}
